package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.QueryCondition;
import com.ruoyi.system.domain.Sumvo;
import com.ruoyi.system.domain.SysUserxq;
import com.ruoyi.system.service.ISysUserxqService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 全员得分Controller
 * 
 * @author ruoyi
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/userxq/userxq")
public class SysUserxqController extends BaseController
{
    @Autowired
    private ISysUserxqService sysUserxqService;

     // 高层  查询某个月的所有成员
    @GetMapping("/all")
    public TableDataInfo selectUserListg(Sumvo sumvo){
        startPage();
        List<Sumvo> sumvos = sysUserxqService.selectUserList(sumvo);
        return getDataTable(sumvos);
    }

    //查询能力和素质测评的每项数据
    @Anonymous
    @RequestMapping("/selectcha")
    public List selectcha(@RequestBody Map map)
    {
        Integer userId1 = Integer.valueOf(map.get("userId").toString());
        //当前登录的用户id存到map中
        //判断是前端传过来的数是0还是用户id，为0就取当前登录的用户id
        if (userId1==0){
            Long userId = SecurityUtils.getUserId();
            map.put("userId",userId);
        }
        List<Map> list = sysUserxqService.selectSumByjc(map);
        return list;
    }

    @Anonymous
    @RequestMapping("/selectcha01")
    public List<Double> selectcha01(@RequestBody Map map)
    {
        Integer userId1 = Integer.valueOf(map.get("userId").toString()) ;
        //当前登录的用户id存到map中
        //判断是前端传过来的数是0还是用户id，为0就取当前登录的用户id
        if (userId1==0){
            Long userId = SecurityUtils.getUserId();
            map.put("userId",userId);
        }
        List<Double> dd=new ArrayList<>();
        map.put("parentId",1);
        List<Map> list = sysUserxqService.selectSumByjc(map);
        Double p1=0.0;//能力总分
        for (Map map1:list){
            p1+=Double.parseDouble(map1.get("score").toString());
        }
        map.put("parentId",7);
        List<Map> list1 = sysUserxqService.selectSumByjc(map);
        Double p2=0.0;//素质总分
        for (Map map2:list1){
            p2+=Double.parseDouble(map2.get("score").toString());
        }
        dd.add(p1);
        dd.add(p2);
        return dd;
    }

    // 查询某个人 给高层 20条 分别打了多少分
    @GetMapping("/alls")
    public TableDataInfo selectUserList(Sumvo sumvo) {
        startPage();
        List list = sysUserxqService.selectContent(sumvo);
        return getDataTable(list);
    }




    /**
     * 导出全员得分列表
     */
    @PreAuthorize("@ss.hasPermi('userxq:userxq:export')")
    @Log(title = "全员得分", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QueryCondition queryCondition)
    {
      PageInfo<SysUserxq> sysUserxqPageInfo = sysUserxqService.selectUserDetails(queryCondition);
    List<SysUserxq> list = sysUserxqPageInfo.getList();  // 从分页对象中获取数据
    ExcelUtil<SysUserxq> util = new ExcelUtil<>(SysUserxq.class);
    util.exportExcel(response, list, "全员得分数据");  // 导出分页数据中的列表

    }
    /**
     * 获取所有用户信息
     *
     * @param queryCondition 查询条件，用于分页或其他查询参数
     * @return 包含用户信息的AjaxResult对象
     */
    @PreAuthorize("@ss.hasPermi('userxq:userxq:list')")
    @GetMapping("/findALLUser")
    public AjaxResult findALLUser(QueryCondition queryCondition)
    {
    	PageInfo<SysUserxq> list = sysUserxqService.selectUserDetails(queryCondition);
    	return success(list);
    }

    /**
     * 获取所有部门信息
     *
     * @return 包含部门信息的AjaxResult对象
     */
    @PreAuthorize("@ss.hasPermi('userxq:userxq:list')")
    @GetMapping("/findALLDept")
    public AjaxResult findALLDept()
    {
    	List<SysUserxq> list = sysUserxqService.selectAllDept();
    	return success(list);
    }

    @GetMapping("/selectrole")
    public Long findrole(){
        //拿到角色id
            Long roleId=null;
            List<SysRole> roles = SecurityUtils.getLoginUser().getUser().getRoles();
            for (SysRole role : roles) {
                roleId=role.getRoleId();
            }
            return roleId;
    }

    //算权重
    @GetMapping("/quan")
    public AjaxResult findbyquanzhong(){
        List maps = sysUserxqService.selectByQuanzhong();
        return success(maps);
    }

    //查询所有用户及其所属部门岗位角色
    @GetMapping("/selectAllUser")
    public List<SysUserxq> selectAllUser(){

        List<SysUserxq> sysUserxqs = sysUserxqService.selectAllUser();
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        System.out.println(sysUserxqs);
        return sysUserxqs;
    }
}
